//
// Created by postgres on 5/9/17.
//

#include <stdlib.h>
#include "../include/common/common.h"
#define MaxTreeSize 100
typedef char DataType;
typedef struct CNode {//子链表结点
    int child; //孩子结点在向量中对应的序号
    struct CNode *next;
} CNode;
typedef struct{
    DataType data; //存放树中结点数据
    CNode *firstChild;//孩子链表的头指针
}PTNode;
typedef struct{
    PTNode node[MaxTreeSize];
    int n,root; //n为结点总数，root指出根在向量中的位置
}CTree;
void CreateCTree(CTree *tree) {
    int i, j,parent;
    char ch;
    //printf("请输入结点个数:\n");
    //scanf("%d",&(tree.n));
    (*tree).n = 10;
    char str[] = "0,a,-1;1,b,0;2,c,0;3,d,1;4,e,2;5,f,2;6,g,3;7,h,3;8,i,3;9,j,4";
    char delimiters1[] = ";";
    char delimiters2[] = ",";
    char *wrk, *rv1, *rv2;
    wrk = str;
    i = 0;
    (*tree).root =0;
    while ((rv1 = str_split(&wrk, delimiters1))) {
        rv2 = str_split(&rv1, delimiters2);
        i = atoi(rv2);
        (*tree).node[i].data = *(str_split(&rv1, delimiters2));
        rv2 = str_split(&rv1, delimiters2);
        parent = atoi(rv2);
        if(parent!=-1){
            CNode *pre=(*tree).node[parent].firstChild;
            if(!pre){
                (*tree).node[parent].firstChild=palloc(sizeof(CNode));
                (*tree).node[parent].firstChild->next=NULL;
                (*tree).node[parent].firstChild->child=i;
            }else{
                while(pre->next)
                    pre=pre->next;
                pre->next=palloc(sizeof(CNode));
                pre->next->child=i;
            }

        }

    }

}
void InitCTree(CTree *tree){
    int i;
    for(i=0;i<MaxTreeSize;i++){
        (*tree).node[i].firstChild=NULL;
        (*tree).n=0;
        (*tree).root=0;
    }
}
void ctree_test() {
    CTree ctree;
    InitCTree(&ctree);
    CreateCTree(&ctree);

}